/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var threeSumClosest = function(nums, target) {
    nums = nums.sort((a, b) => {
        return a - b;
    })
    let target_close = 100001;
    for (let i = 0; i < nums.length; i++) {
        let pb = i + 1;
        let pc = nums.length - 1;
        while (pc > pb) {
            let target_close_help = nums[i] + nums[pb] + nums[pc];
            if (target_close_help < target) {
                pb++;
            } else if (target_close_help > target) {
                pc--;
            } else {
                return target;
            }
            if (Math.abs(target_close_help - target) < Math.abs(target_close - target)) {
                target_close = target_close_help;
            }
        }
    }
    return target_close;

}